package st.business.tepth.business.taskallocation.modifytrouble;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-07-24 19:35
 */

public class ModifyTroubleDelegate extends MvpFragment<ModifyTroublePresenter> implements ModifyTroubleContract.View {

    public static ModifyTroubleDelegate newInstance(int repairId) {
        Bundle bundle = new Bundle();
        bundle.putInt("repairId", repairId);
        ModifyTroubleDelegate delegate = new ModifyTroubleDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private int mRepairId;
    private ModifyTroubleAdapter mAdapter;
    private DialogLoader mDialogLoader;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("repairId");
        }
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public ModifyTroublePresenter getPresenter() {
        return new ModifyTroublePresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_modify_trouble;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 5));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        mAdapter = new ModifyTroubleAdapter(null, new ModifyTroubleAdapter.OnModifyListener() {
            @Override
            public void onModify(int phenomenonId, int targetSeriesId) {
                mDialogLoader.showProgressDialog(true, "正在提交...",
                        "请稍候");
                mPresenter.modifyTroubleClassification(UserManager.getInstance().getToken(),
                        mRepairId, phenomenonId, targetSeriesId);
            }
        });
        mAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                MultipleItemEntity entity = mAdapter.getItem(position);
                return entity == null ? 5 : (entity.getItemType() == ModifyTroubleAdapter.TYPE_1 ?
                        1 : 5);
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.view_loading,
                (ViewGroup) mRecyclerView.getParent());
        mTvTitle.setText("修改报修系别");
        mDialogLoader = new DialogLoader(_mActivity);
        mPresenter.getTroubleList(UserManager.getInstance().getToken(), mRepairId);
    }

    @Override
    public void showData(List<MultipleItemEntity> list) {
        mAdapter.setEmptyView(R.layout.view_search_empty,
                (ViewGroup) mRecyclerView.getParent());
        mAdapter.setNewData(list);
    }

    @Override
    public void modifySucceed() {
        _mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mDialogLoader != null) {
                    mDialogLoader.stopProgressWithSuccess("修改成功", null, new IDialogClickListener() {
                        @Override
                        public void onClick() {
                            mDialogLoader.clearDialog();
                        }
                    });
                }
            }
        });
    }

    @Override
    public void modifyFailed(final String msg) {
        _mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mDialogLoader != null) {
                    mDialogLoader.stopProgressWithFailed("修改失败",
                            msg);
                }
            }
        });
    }
}